odoo.define('td_dynamic_list.custom_list_columns', function (require) {
    "use strict";
    var core = require('web.core')
    var ListController = require('web.ListController');
    // var ListView = require('web.ListView')
    var QWeb = core.qweb;
    var session = require('web.session');
    // var Model = require('web.Model');
    // var data = require('web.data');

    ListController.include({
        init: function () {
            var self = this;
            this._super.apply(this, arguments);
            this.save_columns = this.get_save_columns();
            this.all_field = self.get_all_field_name();
        },
        // start:function () {
        //     var self = this;
        //     // self.my_setup_columns()
        //     // self.immediately_show_columns()
        // },

        renderButtons: function ($node) {
            this._super.apply(this, arguments);
            this.$buttons.on('click', '.oe_select_columns', this.my_setup_columns.bind(this));
            this.$buttons.on('click', '#apply', this.hide_show_columns.bind(this));
            this.$buttons.on('click', '#show-menu', this.stop_event.bind(this));
            // 所有字段
            this.$buttons.on('click', '#allFields', this.all_field_name.bind(this));
            // 全选
            this.$buttons.on('click', '#selectAll', this.select_all.bind(this));
            this.$buttons.on('click', '.item_column', this.immediately_show_columns.bind(this));
            // 保存
            // this.$buttons.on('click', '#Save', this.set_save_columns.bind(this));
        },

        my_setup_columns: function () {
            var self = this;
            $("#show-menu").show();
            // 如果第一次使用,展示默认值
            var columns = []
            this.contents = this.$buttons.find('ul#show-menu');

            if ($.isEmptyObject(this.save_columns) === true) {
                _.each(this.renderer.arch.children, function (node) {
                    console.log(node)
                    console.log(node.attrs)
                    var name = node.attrs.name
                    // var description = node.attrs.string || self.renderer.state.fields[name].string;
                    var description = self.renderer.state.fields[name].string;
                    columns.push({
                        'field_name': node.attrs.name,
                        'label': description,
                        'invisible': node.attrs.modifiers.column_invisible || false
                    })
                })
            } else {
                $.each(self.save_columns, function (name, invisible) {
                    var description = self.renderer.state.fields[name].string;
                    columns.push({
                        'field_name': name,
                        'label': description,
                        'invisible': invisible
                    })
                });
            }
            this.contents.append($(QWeb.render('ColumnSelectionDropDown', {
                widget: this,
                columns: columns
            })));
            self.immediately_show_columns();

        },

        all_field_name: function () {
            //所有的列
            var self = this;
            this.contents = this.$buttons.find('ul#show-menu');
            // TODO 清除所有的内容
            $('.item_column').remove();
            if (this.all_field.length > 0) {
                this.contents.append($(QWeb.render('ColumnSelectionDropDown', {
                    widget: this,
                    columns: this.all_field
                })));
                // self.immediately_show_columns();
            }
        },

        stop_event: function (e) {
            e.stopPropagation();
        },

        hide_show_columns: function () {
            this.set_save_columns()

            $("#show-menu").hide();
            this.immediately_show_columns()
        },
        immediately_show_columns: function () {
            // 立即显示列
            this.setup_columns()
            var state = this.model.get(this.handle);
            this.renderer.updateState(state, {
                reload: true
            })
        },

        select_all: function () {
            var input_checked = document.getElementById("selectAll");
            if (input_checked.checked === true) {
                _.each($("input[name='cb']"), function (item) {
                    if (item.checked === false) {
                        item.checked = true
                    }
                })
            } else {
                _.each($("input[name='cb']"), function (item) {
                    if (item.checked === true) {
                        item.checked = false
                    }
                })
            }
        },

        setup_columns: function () {
            var self = this;
            _.each(this.contents.find('li.item_column'), function (item) {
                var checkbox_item = $(item).find('input');
                var field = _.find(self.renderer.arch.children, function (field) {
                    return field.attrs.name === checkbox_item.data('name')
                });
                field.attrs.modifiers.column_invisible = !checkbox_item.prop('checked');
            })
        },
        set_save_columns: function () {
            //保存
            var self = this;
            var col_info = []
            _.each($("input[name='cb']"), function (item) {
                col_info.push({
                    'column_name': item.getAttribute('data-name'),
                    'invisible': !item.checked,
                })
            });
            var model_info = {'model_name': this.modelName, 'user_id': session.uid, 'column_info': col_info}
            self._rpc({
                model: 'td.dynamic.list',
                method: 'save_dynamic_name',
                kwargs: {
                    model_info: model_info,
                }
            });
        },

        get_save_columns: function () {
            // 获取数据库中存储的列信息
            var save_columns = {};
            var model_name = this.modelName;
            var user_id = session.uid;

            this._rpc({
                model: 'td.dynamic.list',
                method: 'read_dynamic_name',
                kwargs: {
                    model_name: model_name,
                    user_id: user_id,
                }
            }).then(function (res) {
                if (res.length > 0) {
                    _(res).each(function (item) {
                        save_columns[item.column_name] = item.invisible;
                    });
                }

            });
            return save_columns;
        },
        get_all_field_name: function () {
            var all_columns = [];
            var model_name = this.modelName;
            this._rpc({
                model: 'td.dynamic.list',
                method: 'read_all_field_name',
                kwargs: {
                    model_name: model_name
                }
            }).then(function (res) {
                if (res.length > 0) {
                    all_columns.push.apply(all_columns, res);
                }
            });
            return all_columns;
        }

    });


    $(document).click(function () {
        $('#select-columns').click(function () {
            $("#show-menu").show();
        });
        $("#show-menu").hide();
    });


});